#define _CRT_SECURE_NO_WARNINGS 1
#include "Code.h"

void Stack::Init() {
	capacity = 4;
	top = 0;
	a = (int*)malloc(sizeof(int) * capacity);
	if (a == nullptr) {
		perror("malloc failed:\n");
		exit(1);
	}
}

int Stack::Size() {
	return top;
}

void Stack::Push(int x) {
	if (Size() == capacity) {
		int* tmp = (int*)realloc(a, sizeof(int) * (2 * capacity));
		if (tmp == nullptr) {
			perror("realloc failed:\n");
			exit(1);
		}
		a = tmp;
		capacity = 2 * capacity;
	}
	a[top++] = x;
}

void Stack::Pop() {
	assert(top != 0);
	top--;
}

int Stack::Top() {
	assert(top != 0);
	return a[top - 1];
}

void Stack::Destroy() {
	free(a);
	a = nullptr;
	top = capacity = 0;
}

void Stack::Print() {
	assert(top != 0);
	for (int i = 0; i < Size(); i++)
		cout << a[i] << " ";
	cout << endl;
}

int main() {
	Stack st;
	st.Init();
	st.Push(1);
	st.Push(2);
	st.Push(3);
	st.Push(4);
	st.Print();
	return 0;
}